package huadi.concurrent.waitandnotify;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author zhufeng
 *         Date: 12-7-17
 *         Time: 下午6:12
 *         To change this template use File | Settings | File Templates.
 */
class WaitNotify {
	private List<Integer> list = new ArrayList<Integer>();

	public synchronized void push(Integer i) {
		list.add(i);
		System.out.println("push: " + list.size());
		notify();
	}

	public synchronized int pop() {
		if (list.size() < 1) {
			try {
				System.out.println("pop: " + list.size());
				wait(); // list.wait(); wrong!
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else if (list.size() > 100) {
			list.clear();
		}

		System.out.println("pop: before return " + list.size());
		return list.size();
	}
}
